<?php

declare(strict_types=1);

namespace Common;

use App\Http\Common\Controller\AbstractController;
use App\Http\Common\Result;
use Common\trait\ValidatorTrait;
use Hyperf\Context\ApplicationContext;
use Hyperf\HttpServer\Request;
use Hyperf\HttpServer\Response;
use Psr\Http\Message\ResponseInterface;

class CommonController extends AbstractController
{
    use ValidatorTrait;
    private const DEFAULT_PAGE_SIZE = 30;
    private const DEFAULT_PAGE = 1;

    protected function getCurrentPage(): int
    {
        return (int) $this->getRequest()->input('page', self::DEFAULT_PAGE);
    }

    protected function getPageSize(): int
    {
        return (int) $this->getRequest()->input('pageSize', self::DEFAULT_PAGE_SIZE);
    }

    protected function getRequestData(): array
    {
        return $this->getRequest()->all();
    }

    protected function getRequest(): Request
    {
        return ApplicationContext::getContainer()->get(Request::class);
    }

    protected function getResponse(): Response
    {
        return ApplicationContext::getContainer()->get(Response::class);
    }

    protected function valid(array $rules = [], array $message = []) 
    {
        return $this->validate($this->getRequestData(), $rules, $message);
    }

    protected function redirectTo(string $toUrl, int $status = 302, string $schema = 'http'): ResponseInterface
    {
        return $this->getResponse()->redirect($toUrl, $status, $schema);
    }
}
